function lottery(opt){
	this.data = opt.data; //数据
	this.con = opt.con;	//容器
	this.imgList = opt.con.find('#imgList');
	this.namePlay = opt.con.find('#namePlay');
	this.tip = opt.con.find('#tip');
	this.nameArr = [];	//名字容器
	this.nameTimer = null;
	this.imgTimer = null;
	this.init();
	this.addListener();
};
lottery.prototype.init = function(){
	var html = '';
	var _this = this;
	this.namePlay.html('');
	var imgDir = './image/';
	var pieceWidth = 180;//每个图片+margin-right的宽度
	$.each(this.data,function(index,value){
		html = html + '<li id="imgLi'+value.id+'"><img src="'+ imgDir + value.path +'" /></li>';
		_this.nameArr.push(value.name);
	});
	console.log(this.data);
	if(this.data.length == 0){
		alert('全部人数已经中奖');
		this.stateFlag.state = 'end';
		return;
	};
	$.each(this.data,function(index,value){
		html = html + '<li><img src="'+ imgDir + value.path +'" /></li>';
	});
	var imgLsitWidth = 2*_this.data.length*pieceWidth+60;
	_this.imgList.css('width',imgLsitWidth+'px');
	_this.imgList.html(html);
	this.stateFlag = {
		state:'ready',
		imgLsitWidth:imgLsitWidth
	};
	console.log('ready');
	this.setTip(this.stateFlag.state);
};
lottery.prototype.addListener = function(){
	var _this = this;
	$(window).on('keypress',function(ev){
		if(ev.keyCode == 13){
			var state = _this.stateFlag.state;
			if(state == 'ready'){
				_this.run();
			}else if(state == 'run'){
				_this.stop();
			}else if(state == 'stop'){
				_this.reSet();	
				$('.container').removeClass('fireworks');
			}else if(state == 'end'){
				_this.setTip('end');
			};
		};
	});
};
lottery.prototype.run = function(){
	this.stateFlag.state = 'run';
	this.setTip(this.stateFlag.state);
	console.log('run');
	this.playName();
	this.playImg();
};
lottery.prototype.reSet = function(){
	var newData = [];
	var lastId = this.lastId;
	for (var i = 0;i < this.data.length;i++) {
			if(this.data[i].id == lastId){
				
			}else{
				newData.push(this.data[i]);
			};
	};
	this.data = newData;
//	console.log(this.data);
	this.init();
};
lottery.prototype.stop = function(){
	var _this = this;
	_this.stateFlag.state = 'wait';
	setTimeout(function(){	//防止连击enter键
		_this.stateFlag.state = 'stop';
	},1000);
	this.setTip('stop');
	console.log('stop');
	clearInterval(this.nameTimer);
	clearInterval(this.imgTimer);
	this.nameTimer = null;
	this.imgTimer = null;
	this.stopImg();
};
lottery.prototype.playName = function(){
	var _this = this;
	var nameArr = _this.nameArr;
	var namePlay = _this.namePlay;
	var index = 0;
	_this.nameTimer = setInterval(function(){
		namePlay.html(nameArr[index]);
		index ++;
		if(index >= nameArr.length){
			index = 0;
		};
	},100);
};
lottery.prototype.playImg = function(){
	var _this = this;
	var imgList = _this.imgList;
	var left = 0;
	_this.imgTimer = setInterval(function(){
		left = left + 20;
		if(left >= (_this.stateFlag.imgLsitWidth)/2){
			left = 0;
		};
		imgList.css('left',(-left)+'px');
	},20);
};
//停止图片播放
lottery.prototype.stopImg = function(){
	var selName = this.namePlay.html();
	var selObj = null;
	var flag = 0;
	var _this = this;
	if(selName){
		for(var i = 0;i < this.data.length;i++) {
			if(this.data[i].name == selName){
//				(function(x){
//					selObj = this.data[x];
//					flag = x;
//				})(i);
				flag = i;
			};
		};
		selObj = this.data[flag];
		console.log(flag);
		var beforWidth = flag * 180;
		var imgConWidth = this.con.find('.img-con').width();
		var sWidth = Math.round((Number(imgConWidth)-180)/2);
		var cacuLeft = beforWidth -sWidth;
		_this.imgList.animate({
			'left':(-cacuLeft)+'px'
		},1000);
		var selImg = _this.imgList.find('#imgLi'+selObj.id).find('img');
		this.lastId = selObj.id;
		selImg.css({
			width:'200px',
			height:'200px'
		});
		console.log(selObj.id);
		_this.imgList.find('#imgLi'+selObj.id).addClass('sss');
	};
		$('.container').addClass('fireworks');
};
lottery.prototype.setTip = function(flag){
	if(flag == 'ready'){
		this.tip.html('请按Enter键开始抽奖');
	}else if(flag == 'run'){
			this.tip.html('按Enter键停止');
	}else if(flag == 'stop'){
		this.tip.html('完成抽奖,按Enter键重新抽奖');
	}else if(flag == 'end'){
		this.tip.html('抽奖活动已全部结束');
	};
};
